package main

import (
	"fmt"
)

// 闭包

// 累加
func adder() func(value int) int {
	sum := 0
	return func(value int) int {
		sum += value
		return sum
	}
}

type iAdder func(int) (int, iAdder)

func adder2(base int) iAdder {
	return func(v int) (int, iAdder) {
		return base + v, adder2(base + v)
	}
}

func main() {
	adder := adder()
	for i := 0; i < 10; i++ {
		fmt.Printf("adder + %d = %d\t", i, adder(i))
	}

	// 正统的函数式编程
	fmt.Println("正统的函数式编程")
	a := adder2(0)
	for i := 0; i < 10; i++ {
		var s int
		s, a = a(i)
		fmt.Printf("adder2 + %d = %d\t\n", i, s)
	}

}
